/**
 * Created by Administrator on 2016/8/1.
 */


var BaseDao = require('../../base-dao/BaseDao');



//个人头像信息
exports.mineproinfo = function(userid,callback) {
    var sql="select * from tbl_user  where id = '"+userid+"' and status=1";
    BaseDao.execQuery(sql, function(err, rows) {
        callback(err, rows);
    });
};

//个人公司信息
exports.mineprofile = function(userid,callback) {
    var sql="select * from tbl_user  where id = '"+userid+"' and status=1";
    BaseDao.execQuery(sql, function(err, rows) {
        callback(err, rows);
    });
};

//我的订单
exports.mineorder = function(userid,callback) {
    var sql="select per.* from tbl_merchart_perorder per left join tbl_merchart_pay pay on per.payid = pay.id  where pay.userid='"+userid+"' and (orderstatus=3 or orderstatus =1) and per.status=1 and pay.status=1 ";
    BaseDao.execQuery(sql, function(err, rows2) {
        var sql2=" select pay.id,pay.money,ment.payment from tbl_merchart_pay pay left join tbl_merchart_perorder per on pay.id = per.payid  left join tbl_merchart_payment ment on pay.id = ment.payid where pay.userid='"+userid+"' and per.status=1 and pay.status=1 and ment.status =1 group by pay.id";
        BaseDao.execQuery(sql2, function(err, rows1) {
            callback(err, {rows1:rows1,rows2:rows2});
        })
    });
};


//我的意见
exports.mineopinion = function(obj,callback) {
    var sql = 'INSERT INTO tbl_merchart_opinion SET ?';
    BaseDao.execQuery(sql,obj, function(err, rows) {
        callback(err, rows);
    });
};

//管理员订单管理
exports.adminpending = function(userid,callback) {
    var sql1=" select pay.id,pay.money,pay.userid,ment.payment,user.description from tbl_merchart_pay pay left join tbl_merchart_perorder per on pay.id = per.payid  left join tbl_merchart_payment ment on pay.id = ment.payid  left join tbl_user user on user.id = pay.userid where (per.status=1 or per.status=0) and  user.status=1 and pay.status=1 and ment.status =1  group by pay.id";
    BaseDao.execQuery(sql1, function(err, rows1) {
        var sql2="select per.* from tbl_merchart_pay pay left join tbl_merchart_perorder per on pay.id = per.payid where (per.status=1 or per.status=0) and pay.status=1";
        BaseDao.execQuery(sql2, function(err, rows2) {
            callback(err, {rows1:rows1,rows2:rows2});
        });
    });
};

//待处理订单
exports.pendingorder = function(userid,id,callback) {
    var sql="select * from tbl_merchart_perorder where payid = '"+id+"' and userid='"+userid+"' and orderstatus=3 and (status=1 or status=0)";
    BaseDao.execQuery(sql, function(err, rows) {
        callback(err, rows);
    });
};

//删除订单
exports.deleteorder = function(id,userid,callback) {
    var sql = 'UPDATE tbl_merchart_perorder SET status = 0 WHERE payid ="'+ id +'" and userid="'+ userid +'"';
    BaseDao.execQuery(sql, function(err, rows) {
        callback(err, rows);
    });
};

//我的消费
exports.mineexpend = function(userid,callback) {
    var sql='select * from tbl_merchart_perorder where date_format(createAt,"%Y-%m")=date_format(now(),"%Y-%m") and userid = "'+userid+'" and (orderstatus=3 or orderstatus=1) and (status=0 or status=1) ';
    console.log(sql);
    BaseDao.execQuery(sql, function(err, rows) {
        var sql_s='SELECT ment.ordertime,ment.sumprice,sum(ment.payment) payment,pay.createAt,pay.source,sum(pay.money) deposit' +
            ' FROM tbl_merchart_pay pay left join tbl_merchart_payment ment on pay.id =ment.payid WHERE date_format(pay.createAt,"%Y-%m")=date_format(now(),"%Y-%m") and pay.userid = "'+userid+'" AND pay.status = 1 and ment.status=1' +
            ' GROUP BY date_format(pay.createAt, "%Y-%m-%d"),pay.source;';
        console.log(sql_s);
        BaseDao.execQuery(sql_s,function (err,data_s) {
            var allmoney =0;
            for (var j=0;j<data_s.length;j++){
                var createAtMonth=data_s[j].createAt.getMonth()+1;
                var createAtDay=data_s[j].createAt.getDate();
                data_s[j].ordertime = createAtMonth+'月'+createAtDay+'日';
                data_s[j].sumprice = parseFloat(Math.abs((data_s[j].deposit))*10).toFixed(2);
            }

            for(var i=0; i<rows.length;i++){
                allmoney += parseInt(rows[i].count) * (parseFloat(rows[i].price).toFixed(2)) ;
                var rowMonth=rows[i].createAt.getMonth()+1;
                var rowdDay=new Date(rows[i].createAt).getDate();
                rows[i].ordertime = rowMonth+'月'+rowdDay+'日';
            }

            callback(err, {allmoney:allmoney,rows:rows,data_s:data_s});
        })
    });
};
